﻿
j.dialog = function(selector) {
    var t = { selector: selector };
    j.dialog.add(t);
    j.extend(t, { hide: function() { j.dialog.hide(this); } });
    return t;
};
j.extend(j.dialog, {
    dialogs: [],
    startZIndex: 2000,
    hide: function(dialog) {
        if (dialog) {
            this.dialogs.remove(dialog);
        } else {
            this.dialogs.each(function(dialog) { dialog.hide() });
        }
    },
    add: function(dialog) { this.dialogs.push(dialog) }

});




var Dialog = function() {
    var options = arguments[0] || {};
    this.title = options.title || "新窗口",
    this.width = options.width || 400,
    this.height = options.height || 300,
    this.contain = document.createElement("div"),
    this.id = "id" + ((new Date() * Math.random()) >> 0);
    this.init();
}
Dialog.prototype = {
    constructor: Dialog,
    init: function() {
        this.css("body", "background:#000;");
        this.css("body", "color:#fff");
        this.css("p", "border:1px solid red");
    },
    css: function(selector, declaration) {
        if (typeof document.createStyleSheet === 'undefined') {
            document.createStyleSheet = (function() {
                function createStyleSheet() {
                    var element = document.createElement('style');
                    element.type = 'text/css';
                    document.getElementsByTagName('head')[0].appendChild(element);
                    var sheet = document.styleSheets[document.styleSheets.length - 1];
                    if (typeof sheet.addRule === 'undefined')
                        sheet.addRule = function(selectorText, cssText, index) {
                            if (typeof index === 'undefined')
                                index = this.cssRules.length;
                            this.insertRule(selectorText + ' {' + cssText + '}', index);
                        };
                    return sheet;
                }
                return createStyleSheet;
            })();
        }
        if (!!Dialog.sheet) {
            if (!Dialog.memory.exists(selector, declaration)) {
                Dialog.memory.set(selector, declaration);
                Dialog.sheet.addRule(selector, declaration);
            }
        } else {
            Dialog.sheet = document.createStyleSheet();
            var memory = function() {
                var keys = [], values = [], size = 0;
                return {
                    get: function(k) {
                        var results = [];
                        for (var i = 0, l = keys.length; i < l; i++) {
                            if (keys[i] == k) {
                                results.push(values[i])
                            }
                        }
                        return results;
                    },
                    exists: function(k, v) {
                        var vs = this.get(k);
                        for (var i = 0, l = vs.length; i < l; i++) {
                            if (vs[i] == v) {
                                return true;
                            }
                        }
                        return false;
                    },
                    set: function(k, v) {
                        keys.push(k);
                        values.push(v);
                        size++;
                    },
                    length: function() {
                        return size;
                    }
                }
            }
            Dialog.memory = memory();
            Dialog.memory.set(selector, declaration);
            Dialog.sheet.addRule(selector, declaration);
        }
    }
};
window.onload = function() {
    new Dialog(400, 300, "第一个窗口")
    new Dialog(400, 300, "第二个窗口")
}




//Element.showModal = function(node) {
//    Element.hidePopup();
//    if (node != null) {
//        node = $(node);
//        var bcolor = '#383838';
//        if (arguments[1] != null) {
//            bcolor = arguments[1];
//        }
//        var backDiv = document.createElement('div');
//        document.body.appendChild(backDiv);
//        var w = window.clientWidth() < window.contentWidth() ? window.contentWidth() : window.clientWidth();
//        var h = window.clientHeight() < window.contentHeight() ? window.contentHeight() : window.clientHeight();
//        backDiv.id = 'popupBackDiv';
//        //backDiv.style.backgroundColor = bcolor;
//        backDiv.style.position = 'absolute';
//        backDiv.style.width = w + 'px';
//        backDiv.style.height = h + 'px';
//        backDiv.style.left = '0px';
//        backDiv.style.top = '0px';
//        backDiv.style.zIndex = 1;
//        if (Masque.isIE) {
//            backDiv.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50,startX=0,startY=0,finishX=100,finishY=100);';
//        }
//        else {
//            backDiv.style.opacity = 0.5;
//        }
//        Element.show(node);
//        node.style.position = 'absolute';
//        var left = (window.clientWidth() - node.offsetWidth) / 2 + document.documentElement.scrollLeft;
//        var top = (window.clientHeight() - node.offsetHeight) / 2 + document.documentElement.scrollTop;
//        if (node.offsetParent != null) {
//            var p = GetPosition(node.offsetParent);

//            node.style.top = top - p.Top + "px";
//            node.style.left = left - p.Left + "px";
//        } else {
//            node.style.top = top + 'px';
//            node.style.left = left + 'px';
//        }
//        node.style.zIndex = 999;
//        Element.currentPopup = node;

//        var mendIframe = document.createElement('iframe');
//        document.body.appendChild(mendIframe);
//        mendIframe.id = "popupMendIframe";
//        mendIframe.style.border = "0px";
//        mendIframe.style.display = "block";
//        mendIframe.style.position = "absolute";
//        mendIframe.style.zIndex = 900;
//        mendIframe.style.left = left + "px";
//        mendIframe.style.top = top + "px";
//        if (Masque.isIE) {
//            mendIframe.style.height = node.currentStyle.height;
//            mendIframe.style.width = node.currentStyle.width;
//        }
//        else {
//            mendIframe.style.height = node.offsetHeight + "px";
//            mendIframe.style.width = node.offsetWidth + "px";
//        }
//        mendIframe.style.margin = "0px";
//        mendIframe.style.padding = "0px";
//    }
//}